import time
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd

# 打开微博首页
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://weibo.com/')

# 等待用户登录
time.sleep(30)

# 打开热搜榜页面
browser.get('https://s.weibo.com/top/summary?cate=realtimehot')
time.sleep(3)

# 获取网页源代码
html_code = browser.page_source
browser.quit()

# 用BeautifulSoup解析网页源代码
soup = BeautifulSoup(html_code, 'lxml')

# 定位包含序号、关键词、热搜指数的标签
ranks = soup.select('td.td-01')
keywords = soup.select('td.td-02 > a')
indices = soup.select('td.td-02 > span')

# 提取数据并进行清洗和整理
rank_list = [r.get_text().strip() for r in ranks]
keyword_list = [k.get_text().strip() for k in keywords]
index_list = [i.get_text().strip().split(' ')[-1] for i in indices]
index_list = [''] + index_list

# 整理和导出数据
data = {'序号': rank_list, '关键词': keyword_list, '热搜指数': index_list}
df = pd.DataFrame(data)
df.to_csv('新浪微博热搜榜.csv', index=False, encoding='utf-8-sig')
